/**
 * @author songgao
 */
var outputStyleTag = false;
var slaveKey;
var population;

function appendText(content){
    var ele = $("<div>");
    if (outputStyleTag) 
        ele.addClass("output1");
    else 
        ele.addClass("output2");
    outputStyleTag = !outputStyleTag;
    ele.append(document.createTextNode(content));
    $("#output").append(ele);
}

function reg(){
    $.get("clientService", {
        action: "register"
    }, function(xml){
        slaveKey = $(xml).find("RegistrationResult").attr("SlaveKey");
        appendText("Registered, key = " + slaveKey);
    });
}

function getpop(){

    var xmlString = '<GetPopulation SlaveKey="' + slaveKey + '"/>';
    
    $.post("clientService", {
        action: "getpopulation",
        xml: xmlString
    }, function(xml, status, req){
        if (!req.responseXML) {
    		appendText("no availabe population to be evolved.");
			return; 
        }
        population = new Object();
        population.generation = $(xml).find("Population").attr("Generation") * 1;
        population.preKey = $(xml).find("Population").attr("Key");
        population.individuals = new Array();
        $(xml).find("Individual").each(function(){
            var individual = new Object();
            individual.fitness = $(this).attr("Fitness") * 1;
            individual.gene = new Array();
            $(this).find("Gene").each(function(){
                individual.gene.push($(this).attr("Value") * 1);
            });
            population.individuals.push(individual);
        });
        appendText("Population received. Generation = " + population.generation + ", length = " + population.individuals.length + '\n' + population.preKey);
    });
    
}

function evolve(){
    appendText("begin evolving");
    for (var ind in population.individuals) {
        for (var i in population.individuals[ind]) 
            population.individuals[ind].gene[i] = Math.round(Math.random() * 10);
        population.individuals[ind].fitness = Math.round(Math.random() * 10);
    }
    population.generation += Math.round(Math.random() * 20);
    appendText("evolved to " + population.generation);
}

function submitpop(){
    var xmlString = '<SubmitPopulation SlaveKey="' + slaveKey + '">' +
    '<Population Generation="' +
    population.generation +
    '" PredecessorKey="' +
    population.preKey +
    '">';
    for (var ind in population.individuals) {
        xmlString = xmlString +
        '<Individual Fitness="' +
        population.individuals[ind].fitness +
        '">';
        for (var g in ind.gene) 
            xmlString = xmlString = '<Gene Value="' + ind.gene[g] + '"/>';
        xmlString = xmlString + '</Individual>';
    }
    xmlString = xmlString + '</Population></SubmitPopulation>';
    
    $.post("clientService", {
        action: "submitpopulation",
        xml: xmlString
    }, function(xml){
        appendText("response content:" + (new XMLSerializer()).serializeToString(xml));
    });
}

$(document).ready(function(){
    $("#register").click(reg);
    $("#getpop").click(getpop);
    $("#evolve").click(evolve);
    $("#submitpop").click(submitpop);

});
